/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SelectEIR.java
 *
 * Created on Dec 6, 2010, 8:45:45 PM
 */

package lmclogistics;

import components.Image;
import components.PeriodToCover;
import components.Dates;
import components.NButton;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.sql.*;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.AbstractButton;
import javax.swing.JCheckBox;
import javax.swing.UIManager;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import sql.Sql;

/**
 *
 * @author katrina
 */
public class SelectEIR extends javax.swing.JFrame {

    private Image bg;
    private String customer;
    private int cust=0;
    private PeriodToCover ptc;

    private JTable selectTable;
    private JScrollPane scroll;
    private DefaultTableModel model;
    private TableColumn column;
    private int sel;

    /** Creates new form SelectEIR */
    public SelectEIR(String cust, int what) {
        customer = cust;
        sel = what;
        initComponents();

        setTitle("Edit User");
        setAlwaysOnTop(true);
        setSize(512, 488);
        centerScreen();
        setResizable(false);
        setVisible(true);
        setBg();

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLayout(null);

        BufferedImage image = null;
        try {
            image = ImageIO.read(getClass().getResource("/images/favicon.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        setIconImage(image);

        okCancelButtons();
        initTable();
        getCustomer();
    }

    public void setBg(){
        bg = new Image("panel.png",509,460);
        bg.setLocation(0,0);
        add(bg,-1);

    }

    public void centerScreen(){

        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        // Determine the new location of the window
        int w = getSize().width;
        int h = getSize().height;
        int x = (dim.width-w)/2;
        int y = (dim.height-h)/2;

        // Move the window
        setLocation(x, y);
    }

    public void getCustomer()
    {
        String query = "SELECT custCode FROM customer WHERE custName='" + customer + "'";

        try {
            Connection connection = Sql.getSqlConnection();
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(query);

            if(rs.next())
            {
                cust = Integer.parseInt(rs.getString(1));
            }

            Sql.closeSqlConnection(connection);
        } catch (Exception ex) {
            Logger.getLogger(SelectEIR.class.getName()).log(Level.SEVERE, null, ex);
        }

        addRows();
    }
    public void addRows()
    {
        model = (DefaultTableModel) selectTable.getModel();
            String query = "SELECT outCode FROM rental WHERE custID=" + cust + " AND paid=0";
        try {
            Connection connection = Sql.getSqlConnection();
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(query);

            Boolean a = new Boolean(false);
            int i=0;
            while(rs.next())
            {
                Vector rowData = new Vector();
                rowData.add(a);
                rowData.add(rs.getString(1));
                model.addRow(rowData);
            }

            selectTable.setModel(model);

            column = selectTable.getColumnModel().getColumn(0);
            column.setPreferredWidth(225);

            column = selectTable.getColumnModel().getColumn(1);
            column.setPreferredWidth(225);

            Sql.closeSqlConnection(connection);
        } catch (Exception ex) {
            Logger.getLogger(SelectEIR.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void initTable()
    {
                selectTable = new JTable();
                selectTable.setAutoCreateRowSorter(true);
		selectTable.setFont(new Font("Lucida Grande", Font.PLAIN, 12));
		selectTable.getTableHeader().setFont(new Font("Lucida Grande", Font.PLAIN, 12));
		
                scroll = new JScrollPane(selectTable);
		scroll.setBounds(15,15,477,350);
		add(scroll,0);

                selectTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Check", "EIR Out"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Boolean.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                true, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        
        selectTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        selectTable.getTableHeader().setReorderingAllowed(false);
        selectTable.getColumnModel().getColumn(0).setResizable(false);
        selectTable.getColumnModel().getColumn(1).setResizable(false);

    }

    public void okCancelButtons()
    {
        NButton cancel = new NButton("Cancel",410,420);
        NButton ok = new NButton("Ok",320 ,420);
        add(ok,0);
        add(cancel,0);
        cancel.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                        dispose();
                    }
                });
        ok.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    ArrayList a = new ArrayList();
                    for(int i = 0;i<selectTable.getRowCount();i++)
                    {
                        if(selectTable.getValueAt(i, 0).equals(true))
                        {
                            int outCode = Integer.parseInt(selectTable.getValueAt(i,1).toString());
                            a.add(outCode);
                        }
                    }             
                try {
                    if(sel==1)
                    {
                        SalesInvoice.setValues(a);
                    }
                    if(sel==2)
                    {
                        NonInvoice.setValues(a);
                    }
                } catch (Exception ex) {
                    Logger.getLogger(SelectEIR.class.getName()).log(Level.SEVERE, null, ex);
                }
                    dispose();
                    }
                });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}

